using Colorful.CMS.Core;
using Colorful.Database;
using Colorful.Image;
using Colorful.Models;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.International.Converters.PinYinConverter;

namespace Colorful.Apps.MemberSystem.Controllers
{
    [Route("api/ms")]
    public class MemberSystemController : AppMemberController
    {
        private const string TOKEN = "6eb0eef28f394964bd40f6afa39c1e70";
        private const int PAGESIZE = 30;

        [Route("member/list")]
        public IActionResult GetData(string keyword, int page)
        {
            using (var db = DbHelper.GetDb())
            {
                var query = db.Members.GetQuery();
                //foreach (var member in query.ToList())
                //{
                //    db.Members.Update(a => a.Id == member.Id, b => b.Keyword, string.Join("", PinYinHelper.GetPingYin(member.Name).FirstPingYin));
                //}
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.Name.Contains(keyword) || a.CardId.Contains(keyword) || a.Tel.Contains(keyword) || a.Keyword.Contains(keyword));
                var datas = query.OrderByDescending(a => a.CardId).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new { a.Id, a.Name, a.CardId }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        [Route("member/info")]
        public IActionResult GetMember(string id)
        {
            using (var db = DbHelper.GetDb())
            {
                var member = db.Members.FirstOrDefault(a => a.Id == id);
                return this.GetResult(true, member);
            }
        }
        [Route("member/save")]
        public IActionResult SaveData(string id, string cardId)
        {
            if (string.IsNullOrEmpty(cardId))
                return this.GetResult(false, "会员卡号不能为空!");
            try
            {
                var isNew = string.IsNullOrEmpty(id);
                using (var db = DbHelper.GetDb())
                {
                    if (isNew && db.Members.Any(a => a.CardId == cardId))
                        return this.GetResult(false, "会员卡号已存在!");
                    var member = db.Members.FirstOrDefault(a => a.Id == id);
                    if (isNew)
                    {
                        member = new Models.Member();
                    }
                    FormHelper.FillTo(member);
                    db.Members.Save(member);
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        [Route("member/cardtype")]
        public IActionResult GetCardType()
        {
            return this.GetResult(true, EnumHelper.ToJsonData<CardType>());
        }
    }
}
